package com.example.mymicaihu.adapter.home;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.mvplibrary.adapter.BaseAdapter;
import com.example.mvplibrary.adapter.BaseViewHolder;
import com.example.mymicaihu.R;
import com.example.mymicaihu.bean.MessageBean;
import com.example.mymicaihu.bean.VideoBean;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;


/**
 * 作者：朱勇闯
 * data : 2021/4/25  17:02
 * 作用： xxxx
 */
public class MessageAdapter extends BaseAdapter<MessageBean.DataBean> {
    private static final String TAG = "朱勇闯videoAdapter";
    public ItemListener itemListener;

    public void setItemListener(ItemListener itemListener) {
        this.itemListener = itemListener;
    }

    public MessageAdapter(List<MessageBean.DataBean> datas, Context context, int layoutId) {
        super(datas, context, layoutId);
    }

    @Override
    protected void bindData(BaseViewHolder holder, MessageBean.DataBean listBean, int position) {
        TextView title = holder.getView(R.id.tv_title_message);
        TextView desc = holder.getView(R.id.tv_desc_message);
        ImageView image = holder.getView(R.id.iv_item_message);

        title.setText(listBean.getName());
        desc.setText(listBean.getPrompt());


        Glide.with(context).load(listBean.getIcon()).into(image);
    }


    public interface ItemListener {
        void onItemClickListener(int pos);
    }


    public static Bitmap createVideoThumbnail(String filePath) {
        // MediaMetadataRetriever is available on API Level 8
        // but is hidden until API Level 10
        Class<?> clazz = null;
        Object instance = null;
        try {
            clazz = Class.forName("android.media.MediaMetadataRetriever");
            instance = clazz.newInstance();

            Method method = clazz.getMethod("setDataSource", String.class);
            method.invoke(instance, filePath);

            // The method name changes between API Level 9 and 10.
            if (Build.VERSION.SDK_INT <= 9) {
                return (Bitmap) clazz.getMethod("captureFrame").invoke(instance);
            } else {
                byte[] data = (byte[]) clazz.getMethod("getEmbeddedPicture").invoke(instance);
                if (data != null) {
                    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                    if (bitmap != null) return bitmap;
                }
                return (Bitmap) clazz.getMethod("getFrameAtTime").invoke(instance);
            }
        } catch (IllegalArgumentException ex) {
            // Assume this is a corrupt video file
        } catch (RuntimeException ex) {
            // Assume this is a corrupt video file.
        } catch (InstantiationException e) {
            Log.e(TAG, "createVideoThumbnail", e);
        } catch (InvocationTargetException e) {
            Log.e(TAG, "createVideoThumbnail", e);
        } catch (ClassNotFoundException e) {
            Log.e(TAG, "createVideoThumbnail", e);
        } catch (NoSuchMethodException e) {
            Log.e(TAG, "createVideoThumbnail", e);
        } catch (IllegalAccessException e) {
            Log.e(TAG, "createVideoThumbnail", e);
        } finally {
            try {
                if (instance != null) {
                    clazz.getMethod("release").invoke(instance);
                }
            } catch (Exception ignored) {
            }
        }
        return null;
    }
}
